﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ImageUploader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private static Socket socketClient = null;
        private void btn_Connect_Click(object sender, EventArgs e)
        {
            IPAddress address = IPAddress.Parse(txt_localIP.Text.Trim());
            IPEndPoint endpoint = new IPEndPoint(address, int.Parse(txt_localPort.Text.Trim()));
            socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socketClient.Connect(endpoint);
            if (socketClient.Connected)
            {
                showMsg(socketClient.RemoteEndPoint + "连接成功");
            }
        }

        private void showMsg(string msg)
        {
            this.txt_msg.Text = msg + "\r\n";
        }

        private void btn_selectFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if(ofd.ShowDialog() == DialogResult.OK)
            {
                txt_FileName.Text = ofd.FileName;
            }
        }

        private void btn_send_Click(object sender, EventArgs e)
        {
            int i = Net.SendFile(socketClient, txt_FileName.Text, 512, 1);
            if(i == 0)
            {
                showMsg(txt_FileName.Text + "文件发送成功");
                socketClient.Close();                
                showMsg("连接关闭");
            }
            else
            {
                showMsg(txt_FileName.Text + "文件发送失败，i=" + i);
            }
        }


       
    }
}
